#include "head.h"

// 单链表的创建
U_list *S_List_init()
{
    // 创建头节点
    U_list *list = (U_list *)malloc(sizeof(U_list));
    // 判断是否创建成功
    if (list != NULL)
    {
        // 如果成功则初始化链表中的头节点为 NULL
        list->next = NULL;
    }
    else
    {
        printf("创建失败\n");
        return NULL;
    }
    // 返回链表结构体
    return list;
}

// 单链表的插入
void S_List_insert(U_list *list, U_data data, int pos)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }

    U_node *new_node = (U_node *)malloc(sizeof(U_node));
    if (new_node == NULL)
    {
        printf("new_node创建失败\n");
        return;
    }
    new_node->next = NULL;
    new_node->data = data;

    if (pos == 1)
    {
        new_node->next = list->next;
        list->next = new_node;
        return;
    }

    U_node *ptr = list->next;

    for (size_t i = 1; i < pos - 1 && ptr != NULL; i++)
    {
        ptr = ptr->next;
    }

    if (ptr == NULL)
    {
        printf("ptr已经指向NULL\n");
        free(new_node);
        return;
    }
    new_node->next = ptr->next;
    ptr->next = new_node;

    return;
}

// 单链表的尾插
void tail_insert_node(U_list *list, U_data data)
{
    if (NULL == list)
    {
        printf("链表 为 NULL\n");
        return;
    }
    U_node *new_node = (U_node *)malloc(sizeof(U_node));
    new_node->data = data; // 设置新节点的数据
    new_node->next = NULL; // 将新节点的指针指向 NULL，因为它将成为最后一个节点

    if (NULL == list->next) // 如果待链接结点是该链表中的第一个有数据的结点，则头结点直接链接上新的结点
    {
        list->next = new_node;
    }
    else // 否则就遍历链表，将指针定位到链表结尾进行插入
    {
        U_node *node1 = list->next;
        for (size_t i = 0; node1->next != NULL; i++)
        {
            node1 = node1->next;
        }
        node1->next = new_node;
    }
}

// 单链表的删除
void S_List_delete(U_list *list, int pos)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }

    U_node *ptr_1 = list->next;
    U_node *ptr_2 = NULL;
    if (pos == 1)
    {
        list->next = ptr_1->next;
        free(ptr_1);
        return;
    }

    for (size_t i = 0; i < pos - 1 && ptr_1->next != NULL; i++)
    {
        ptr_2 = ptr_1;
        ptr_1 = ptr_1->next;
    }
    if (ptr_1 == NULL)
    {
        printf("ptr_1指向null\n");
        return;
    }
    ptr_2->next = ptr_1->next;

    free(ptr_1);
    return;
}

// 单链表的查找 用户名
U_node *S_List_find_name(U_list *list, char *name)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return NULL;
    }

    U_node *ptr = list->next;
    while (ptr != NULL)
    {
        if (strcmp(ptr->data.name, name) == 0)
        {
            return ptr;
        }
        ptr = ptr->next;
    }

    return NULL;
}


// 单链表的遍历
void S_List_print(U_list *list)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }
    U_node *ptr = list->next;
    while (ptr != NULL)
    {
        printf("用户名:%s\t密码:%s\t身份码:%d\t余额:%d\n",
               ptr->data.name, ptr->data.passwd, ptr->data.grade, ptr->data.value);
        ptr = ptr->next;
    }
}

// 单链表的清空
void S_List_clear(U_list *list)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }

    U_node *ptr_1 = list->next;
    U_node *ptr_2 = ptr_1->next;

    while (ptr_1 != NULL)
    {
        free(ptr_1);
        ptr_1 = ptr_2;
        if (ptr_2 != NULL)
        {
            ptr_2 = ptr_2->next;
        }
    }
    list->next = NULL;
}

// 单链表的合并
U_list *S_List_merge(U_list *list1, U_list *list2)
{
    if (list1 == NULL || list2 == NULL)
    {
        printf("链表不存在\n");
        return list1;
    }

    // 1、求链表list1 的链表长度
    U_node *ptr_1 = list1->next;
    int len = 0;

    while (ptr_1 != NULL)
    {
        ptr_1 = ptr_1->next;
        len++;
    }
    // 插入到链表中
    ptr_1 = list1->next;
    U_node *ptr_2 = list2->next;
    while (ptr_2 != NULL)
    {
        len++;
        S_List_insert(list1, ptr_2->data, len);
        ptr_2 = ptr_2->next;
    }

    return list1;
}

// 单链表的销毁
void S_List_free(U_list **list)
{
    if (list == NULL || *list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }

    U_node *ptr_1 = (*list)->next;
    U_node *ptr_2 = ptr_1->next;

    while (ptr_1 != NULL)
    {
        free(ptr_1);
        ptr_1 = ptr_2;
        if (ptr_2 != NULL)
        {
            ptr_2 = ptr_2->next;
        }
    }

    free(*list);
    *list = NULL;
}

// 写入
void S_List_File_print(U_list *list, const char *file_name)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }

    FILE *fp = fopen(file_name, "w");
    if (fp == NULL)
    {
        printf("文件创建失败\n");
        return;
    }

    U_node *ptr = list->next;
    while (ptr != NULL)
    {
        fprintf(fp, "用户名:%s\t密码:%s\t身份码:%d\t余额:%d\t累计充值:%d\t订单数:%d\n",
                ptr->data.name, ptr->data.passwd, ptr->data.grade, ptr->data.value, ptr->data.recharge, ptr->data.order);

        for (int i = 0; i < ptr->data.order; i++)
        {
            fprintf(fp, "%d\t%s\t%s\t%s\t%d:%d\t%d:%d\t%s\t%d\t%d\t%d\t%c\n",
                    ptr->data.ticket[i].id,
                    ptr->data.ticket[i].flighttype,
                    ptr->data.ticket[i].startcity,
                    ptr->data.ticket[i].arrivecity,
                    ptr->data.ticket[i].starttime[0], ptr->data.ticket[i].starttime[1],
                    ptr->data.ticket[i].arrivetime[0], ptr->data.ticket[i].arrivetime[1],
                    ptr->data.ticket[i].flytime,
                    ptr->data.ticket[i].value,
                    ptr->data.ticket[i].maxNUM,
                    ptr->data.ticket[i].leftbuyersa,
                    ptr->data.ticket[i].whetherfly);
        }

        ptr = ptr->next;
    }

    fclose(fp);
}

// 读取
void S_List_File_scanf(U_list *list, const char *file_name)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }

    FILE *fp = fopen(file_name, "r");
    if (fp == NULL)
    {
        printf("文件打开失败\n");
        return;
    }

    // 清空链表
    S_List_clear(list);

    U_data data;

    // 移动光标到文件开始
    fseek(fp, 0, SEEK_SET);

    char ch = 0;
    int pos = 0;

    // 检测文件行数
    while ((ch = fgetc(fp)) != EOF)
    {
        if (ch == '\n')
        {
            pos++;
        }
    }

    // 移动光标
    fseek(fp, 0, SEEK_SET);

    for (int i = 0; i < pos; i++)
    {
        fscanf(fp, "用户名:%15s\t密码:%63s\t身份码:%d\t余额:%d\t累计充值:%d\t订单数:%d\n",
               data.name, data.passwd, &data.grade, &data.value, &data.recharge, &data.order);

        for (size_t j = 0; j < data.order; j++)
        {
            fscanf(fp, "%d\t%19s\t%19s\t%19s\t%d:%d\t%d:%d\t%19s\t%d\t%d\t%d\t%c\n",
                   &data.ticket[j].id,
                   data.ticket[j].flighttype,
                   data.ticket[j].startcity,
                   data.ticket[j].arrivecity,
                   &data.ticket[j].starttime[0], &data.ticket[j].starttime[1],
                   &data.ticket[j].arrivetime[0], &data.ticket[j].arrivetime[1],
                   data.ticket[j].flytime,
                   &data.ticket[j].value,
                   &data.ticket[j].maxNUM,
                   &data.ticket[j].leftbuyersa,
                   &data.ticket[j].whetherfly);
            pos--;
        }

        S_List_insert(list, data, i + 1);
    }
    fclose(fp);
}